#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
//#include "user_bt_a2dp.h"
//#include "user_bt_common.h"

#define     PREV_IMG_SRC            IMG_MUSIC_PRE_2
#define     NEXT_IMG_SRC            IMG_MUSIC_NEXT_2
#define     PLAY_IMG_SRC            IMG_MUSIC_START_2
#define     PAUSE_IMG_SRC           IMG_MUSIC_PAUSE_2
#define     VOLUME_DEC_SRC          IMG_MUSIC_VOL_1
#define     VOLUME_ADD_SRC          IMG_MUSIC_VOL_2

lv_obj_t * obj_song_context;
lv_timer_t * music_state_timer;
static lv_anim_t a;
//0:pause,1:play
static bool play_pause_state;

static void music_context_event_cb(lv_event_t *e)
{

    lv_event_code_t code = lv_event_get_code(e);
	//lv_obj_t *kb = lv_event_get_user_data(e);
    lv_obj_t *obj = lv_event_get_target(e);
    //printf("%s \r\n",__func__);

//    uint8_t *music_context = user_bt_get_music_songname();
//
//    if(code == LV_EVENT_MSUIC_CONTEXT_IN)
//    {
//
//        if(!lv_obj_is_valid(obj))
//            return;
//
//        if(music_context != NULL)
//        {
//            //lv_label_set_text(obj, music_context);
//            lv_label_set_text_fmt(obj,"%s",music_context);
//            //lv_label_set_text_fmt(obj,"%s",bt_media.song[0]);
//
//            lv_point_t size;
//            lv_label_t * label = (lv_label_t *)obj;
//            lv_txt_get_size(&size, label->text, LV_FONT_ARIALUNI_ALL_32PX, 0, 0,LV_COORD_MAX, LV_TEXT_FLAG_NONE);
//            //printf("txt size x :%d \n",size.x);
//            if(size.x <= 31 * 8)
//            {
//                lv_obj_align(obj,LV_ALIGN_CENTER,(31 * 8 - size.x) / 2,-120);
//            }
//            else
//                lv_obj_align(obj,LV_ALIGN_CENTER,0,-120);
//
//        }
//
//    }

}



static void music_play_pause_event_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_obj_get_child(e->target,0);
     play_pause_state = !play_pause_state;
//    play_pause_state = user_bt_get_music_playstatus();

    if(!lv_obj_is_valid(obj))
        return;

    if(play_pause_state)
    {
        lv_img_set_src(obj,PLAY_IMG_SRC);
        //user_bt_music_control(BT_ENUM_MUSIC_PAUSE);
        //user_bt_set_music_playstatus(false);

    }
    else
    {
        lv_img_set_src(obj,PAUSE_IMG_SRC);
        //user_bt_music_control(BT_ENUM_MUSIC_PLAY);
        //user_bt_set_music_playstatus(true);
    }
}

static void music_pre_next_event_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_obj_get_child(e->target,0);
    uint8_t id    = lv_obj_get_child_id(lv_event_get_target(e));
    //printf("%s %d \r\n", __func__,id);

//    switch(id)
//    {
//        case 1:
//        user_bt_music_control(BT_ENUM_MUSIC_PREVIOUS);
//        break;
//
//        case 2:
//        user_bt_music_control(BT_ENUM_MUSIC_NEXT);
//        break;
//    }

}


static void volume_dec_add_event_cb(lv_event_t *e)
{
    uint8_t id = lv_obj_get_child_id(e->target);
    lv_obj_t *bar = lv_obj_get_child(e->target->parent,6+3);
    uint8_t bar_value = lv_bar_get_value(bar);
    uint8_t vol;
    printf("%s %d %d\r\n",__func__,id,bar_value);
    switch (id)
    {
    case 4+3:
        //volume dec
        if((bar_value-10) <= 0)
            bar_value = 0;
        else
            bar_value -= 10;


        // lv_anim_start(&a);
        break;
    case 5+3:
        //volume add
        bar_value += 10;
        if(bar_value >= 100)
            bar_value = 100;
        // lv_anim_start(&a);

        break;
    default:
        break;
    }

//    vol = user_bt_volume_lev_convert(BT_VOL_MEDIA, bar_value);
//    user_bt_set_spk_volume(BT_VOL_MEDIA, vol);

    printf("bar_value %d\r\n", bar_value);

    if(!lv_obj_is_valid(bar))
        return;

    lv_bar_set_value(bar,bar_value,LV_ANIM_ON);
}

static void music_state_timer_cb(lv_timer_t * timer)
{
    lv_obj_t * obj = timer->user_data;
    lv_obj_t *parent = lv_obj_get_parent(obj);
    uint8_t cnt = lv_obj_get_child_cnt(parent);
    printf("--->%s  %d\r\n", __func__,cnt);
    if(obj != NULL)
    {
//        play_pause_state = user_bt_get_music_playstatus();
        if(play_pause_state)
        {
            //lv_img_set_src(obj,PLAY_IMG_SRC);
        }
        else
        {
            //lv_img_set_src(obj,PAUSE_IMG_SRC);
        }
    }
}

static void bar_op_change_cb(void * var, int32_t v)
{
    // lv_obj_set_x(var, v);
    // printf("v:%d\r\n",v);
    // lv_obj_set_style_bg_opa(var,v,0);
    lv_obj_set_style_opa(var,v,0);
    // lv_obj_set_y(var, v);
}

static void page_event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);
	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_DELETE)
	{

	}else if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        switch(*key)
        {
        case KEY1_CODE:
				user_jump_page(fr_lv_instrument_panel_func);
             break;
        case KEY3_CODE:
				 
             break;
        case KEY5_CODE: //return
				user_jump_page(fr_list_page);
            break;
        }
		printf("key_code %d \n", *key);
	}
}

void fr_lv_app_music_control_func(lv_obj_t *parent,lv_point_t *top)
{
    if(!lv_obj_is_valid(parent))
        return;
    UI_PARENT_INIT(parent);


    lv_obj_t *cont1 = lv_obj_create(parent);
    lv_obj_remove_style_all(cont1);
    lv_obj_clear_flag(cont1, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_scrollbar_mode(cont1, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_size(cont1,LV_PCT(100),40);
    lv_obj_align(cont1, LV_ALIGN_TOP_LEFT, 0, 0);

    lv_obj_t* title_label = lv_label_create(cont1);
    lv_label_set_text(title_label,"音乐");
    lv_obj_set_style_text_font (title_label, LV_FONT_ARIALUNI_ALL_32PX, 0);
    lv_obj_set_style_text_color (title_label, lv_color_hex(0xffffff),0);
    lv_obj_align(title_label, LV_ALIGN_LEFT_MID, 0, 0);

    //时间标签
    lv_obj_t* top_time = lv_label_create(cont1);
    lv_label_set_text_fmt (top_time, "%02d:%02d",10,28);
    lv_obj_set_style_text_color (top_time, lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font (top_time,LV_FONT_MEDIUM_NUMBER_36PX, 0);
    lv_obj_align(top_time, LV_ALIGN_RIGHT_MID, -20, 0);

    lv_obj_t *bg_img1 = lv_img_create(parent);//1
	lv_img_set_src(bg_img1,IMG_MUSIC_BG_1);
	lv_obj_align(bg_img1,LV_ALIGN_TOP_LEFT,103,12);
    lv_obj_t *bg_img2 = lv_img_create(parent);//1
	lv_img_set_src(bg_img2,IMG_MUSIC_BG_2);
	lv_obj_align_to(bg_img2,bg_img1,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);
    lv_obj_t *bg_img3 = lv_img_create(parent);//1
	lv_img_set_src(bg_img3,IMG_MUSIC_BG_3);
	lv_obj_align_to(bg_img3,bg_img2,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);

    //prev
    lv_obj_t *prev_btn = lv_btn_create(parent);
    lv_obj_set_style_pad_all(prev_btn,0,0);
    lv_obj_set_style_bg_color(prev_btn,lv_color_white(),0);
    lv_obj_set_style_bg_opa(prev_btn,LV_OPA_50,0);
    lv_obj_set_style_radius(prev_btn,LV_RADIUS_CIRCLE,0);
    lv_obj_set_size(prev_btn,78,78);
//    lv_obj_set_pos(prev_btn,18,185);
    lv_obj_align(prev_btn, LV_ALIGN_LEFT_MID, 40, 10);

    lv_obj_t *prev_btn_img = lv_img_create(prev_btn);
    lv_img_set_src(prev_btn_img,PREV_IMG_SRC);
    lv_obj_center(prev_btn_img);
    lv_obj_add_event_cb(prev_btn,music_pre_next_event_cb,LV_EVENT_CLICKED,NULL);


    //next
    lv_obj_t *next_btn = lv_btn_create(parent);
    lv_obj_set_style_pad_all(next_btn,0,0);
    lv_obj_set_style_bg_color(next_btn,lv_color_white(),0);
    lv_obj_set_style_bg_opa(next_btn,LV_OPA_50,0);
    lv_obj_set_style_radius(next_btn,LV_RADIUS_CIRCLE,0);
    lv_obj_set_size(next_btn,78,78);
//    lv_obj_set_pos(next_btn,272,185);
    lv_obj_align(next_btn, LV_ALIGN_RIGHT_MID, -40, 10);

    lv_obj_t *next_btn_img = lv_img_create(next_btn);
    lv_img_set_src(next_btn_img,NEXT_IMG_SRC);
    lv_obj_center(next_btn_img);
    lv_obj_add_event_cb(next_btn,music_pre_next_event_cb,LV_EVENT_CLICKED,NULL);

    //play
    lv_obj_t *play_btn = lv_btn_create(parent);
    lv_obj_set_style_pad_all(play_btn,0,0);
    lv_obj_set_style_bg_color(play_btn,lv_color_white(),0);
    lv_obj_set_style_bg_opa(play_btn,LV_OPA_20,0);
    lv_obj_set_style_border_color(play_btn,lv_color_white(),0);
    lv_obj_set_style_border_width(play_btn,5,0);
    lv_obj_set_style_radius(play_btn,LV_RADIUS_CIRCLE,0);
    lv_obj_set_size(play_btn,140,140);
    lv_obj_align(play_btn,LV_ALIGN_CENTER,0,10);
    lv_obj_add_event_cb(play_btn,music_play_pause_event_cb,LV_EVENT_CLICKED,NULL);

    lv_obj_t *play_btn_img = lv_img_create(play_btn);

//    play_pause_state = user_bt_get_music_playstatus();
    if(play_pause_state)
        lv_img_set_src(play_btn_img,PAUSE_IMG_SRC);
    else
        lv_img_set_src(play_btn_img,PLAY_IMG_SRC);

    lv_obj_center(play_btn_img);

    //=====================================================================
    //volume_dec
    lv_obj_t *volume_dec_img = lv_img_create(parent);
    lv_img_set_src(volume_dec_img,VOLUME_DEC_SRC);
    lv_obj_add_flag(volume_dec_img,LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_pos(volume_dec_img,90,212);

    //volume_add
    lv_obj_t *volume_add_img = lv_img_create(parent);
    lv_img_set_src(volume_add_img,VOLUME_ADD_SRC);
    lv_obj_add_flag(volume_add_img,LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_pos(volume_add_img,356,212);

    lv_obj_t * bar = lv_bar_create(parent);
    // lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(bar,lv_color_white(),LV_PART_INDICATOR);
    // lv_obj_set_style_bg_color(bar,lv_color_hex(0xFFB79A),LV_PART_MAIN);
    // lv_obj_set_style_bg_color(bar,lv_palette_main(LV_PALETTE_BLUE),LV_PART_MAIN);
    // lv_obj_set_style_opa(bar,0,0);
    lv_bar_set_range(bar, 0, 100);
    lv_obj_align(bar,LV_ALIGN_TOP_MID,0,235);
    lv_obj_set_size(bar, 188, 12);

    uint8_t vol = 50;//user_bt_get_music_vol() * 100 / 127;
    lv_bar_set_value(bar, vol, LV_ANIM_ON);
    lv_obj_add_event_cb(volume_dec_img,volume_dec_add_event_cb,LV_EVENT_CLICKED,NULL);
    lv_obj_add_event_cb(volume_add_img,volume_dec_add_event_cb,LV_EVENT_CLICKED,NULL);


	obj_song_context = lv_label_create(parent);
	lv_obj_set_style_text_font(obj_song_context,LV_FONT_ARIALUNI_ALL_32PX,0);
	lv_label_set_text(obj_song_context, "1335893839663");
//    lv_label_set_text_fmt(obj_song_context,"%s",user_bt_get_music_songname());
    //lv_obj_align_to(obj_song_context,bg_img1,LV_ALIGN_OUT_BOTTOM_LEFT,0,-8);
	lv_obj_align(obj_song_context,LV_ALIGN_CENTER,20,-82);

	lv_obj_set_width(obj_song_context, LV_PCT(60));
    //lv_label_set_long_mode(obj_song_context, LV_LABEL_LONG_SCROLL);            /*Automatically break long lines*/

	//lv_obj_set_scrollbar_mode(obj_song_context,LV_SCROLLBAR_MODE_OFF);
    lv_obj_add_event_cb(obj_song_context,music_context_event_cb,LV_EVENT_ALL,NULL);//
	lv_obj_add_event_cb(parent, page_event_handler, LV_EVENT_KEY, NULL);
    //music_state_timer = lv_timer_create(music_state_timer_cb, 1000, parent);

#if 0
    // lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, bar);
    lv_anim_set_values(&a, 0, 255);
    lv_anim_set_time(&a, 500);
    lv_anim_set_playback_delay(&a, 2000);
    lv_anim_set_playback_time(&a, 500);
    // lv_anim_set_repeat_delay(&a, 500);
    // lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_path_cb(&a, lv_anim_path_linear);

    lv_anim_set_exec_cb(&a, bar_op_change_cb);
#endif
}
